package zdream.rockchronicle.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.Objects;

import zdream.control.foe.mm.MegaMan;
import zdream.control.profile.IProfile;
import zdream.rockchronicle.RockChronicle;
import zdream.rockchronicle.core.input.InputCenter;
import zdream.rockchronicle.screen.menu.GameMenu;

public class LevelScreen implements Screen {

	final RockChronicle app;
	public GameMenu menu;

	public LevelScreen(RockChronicle app) {
		Gdx.app.log(getClass().getSimpleName(), "启动");

		this.app = Objects.requireNonNull(app);
		menu = new GameMenu(this.app);
		menu.init();
	}

	@Override
	public void show() {
		final IProfile profile1 = app.profiles.getProfile(MegaMan.class.getSimpleName());
		profile1.setInput(app.input.p1);
		app.levels.setPlayer1Profile(profile1);
		app.graphics.enableAndPrepare();
		// tiled 地图
		app.storage.use("mm1cut");
	}

	@Override
	public void render(float delta) {
		if (InputCenter.Q_DEBUG) {
			System.out.println("按 Q 进行 DEBUG");
			InputCenter.Q_DEBUG = false;
		}

		float realDelta = Math.min(delta, 0.1f);
		app.levels.render(realDelta);
		menu.tick(realDelta);

		SpriteBatch uiBatch = app.graphics.uiBatch;
		uiBatch.begin();
		MegaMan p1 = (MegaMan) app.levels.player1;
		String p1Info = "", p1Info2 = "";
		if (p1 != null) {
			p1Info = String.format("(%4.2f,%4.2f) %s%s%s%s | %s",
					p1.anchorX, p1.anchorY,
					p1.leftTouched ? 'L' : '_',
					p1.rightTouched ? 'R' : '_',
					p1.topTouched ? 'T' : '_',
					p1.bottomTouched ? 'B' : '_',
					p1.inAir ? 'A' : 'G');
			p1Info2 = String.format("J:%4.2f %s%s C:%d%s S:%d",
					p1.jumpModule.jumpVy,
					p1.jumpModule.jumpStart ? 'S' : ' ',
					p1.jumpModule.jumpEnd ? 'E' : ' ',
					p1.motionModule.climbing,
					p1.motionModule.climbDirection == 0 ? ' ' : p1.motionModule.climbDirection > 0 ? '+' : '-',
					p1.motionModule.slideDuration
			);
		}
		String debugText = String.format("帧率: %d, 实体: %d, 时间: %4.2f/%4.2f\n%s\n%s",
				Gdx.graphics.getFramesPerSecond(),
				app.levels.foes.size(),
				app.levels.ticker.worldTime / 1000f,
				app.levels.ticker.activeTime / 1000f,
				p1Info, p1Info2);
		app.graphics.font16.setColor(Color.BLACK);
		app.graphics.font16.draw(uiBatch, debugText, 12, 58);
		app.graphics.font16.setColor(Color.WHITE);
		app.graphics.font16.draw(uiBatch, debugText, 10, 60);
		uiBatch.end();
	}

	@Override
	public void resize(int width, int height) {
		// 改变窗口大小
	}

	@Override
	public void pause() {
		// 应该是游戏整体暂停, 而非只有世界暂停
		menu.open();
	}

	@Override
	public void resume() {
		// 现在需要按 MAP_BACK （一般是 ESC 键）恢复
		// app.runtime.worldResume(); 
	}

	@Override
	public void hide() {
//		app.runtime.pauseWorld();
	}

	@Override
	public void dispose() {
		// 仅处理资源回收. 请把逻辑相关的收尾工作放在 willDispose 方法中
	}
}
